const ArticleModel = require('../../model/ArticleModel')
const CategoryModel = require('../../model/CategoryModel')
const TagModel = require('../../model/TagModel')
const ImageModel = require('../../model/ImageModel')
const TrendsModel = require('../../model/TrendsModel')
const TrendsCommentModel = require('../../model/TrendsComment')
const JournalModel = require('../../model/JournalModel')
const VisitLogModel = require('../../model/VisitLogModel')

const dashboardService = {
    // 当前用户的统计数据
    statistics: async(userID) => {
        // 定义统计对象
        const statistics = {
            articleNum: 0,
            categoryNum: 0,
            tagNum: 0,
            pictureNum:0,
            trendsNum: 0,
            trendsCommentNum: 0,
            articleCommentNum: 0
        }
        // 获取当前用户的文章数量
        statistics.articleNum = await ArticleModel.countDocuments({ authorID: userID })
        // 获取当前用户的分类数量
        statistics.categoryNum = await CategoryModel.countDocuments({ userID })
        // 获取当前用户的标签数量
        statistics.tagNum = await TagModel.countDocuments({ userID })
        // 获取当前用户的图片数量
        statistics.pictureNum = await ImageModel.countDocuments({ userID })
        // 获取当前用户的动态数量
        statistics.trendsNum = await TrendsModel.countDocuments({ userID })
        // 获取当前用户的动态评论数量
        statistics.trendsCommentNum = await TrendsCommentModel.countDocuments({ userID })
        return statistics
    },
    // 全局统计数据（所有用户）
    globalStatistics: async() => {
        // 定义统计对象
        const statistics = {
            articleNum: 0,
            categoryNum: 0,
            tagNum: 0,
            pictureNum:0,
            trendsNum: 0,
            trendsCommentNum: 0,
            articleCommentNum: 0,
            userNum: 0
        }
        // 获取所有文章数量
        statistics.articleNum = await ArticleModel.countDocuments()
        // 获取所有分类数量
        statistics.categoryNum = await CategoryModel.countDocuments()
        // 获取所有标签数量
        statistics.tagNum = await TagModel.countDocuments()
        // 获取所有图片数量
        statistics.pictureNum = await ImageModel.countDocuments()
        // 获取所有动态数量
        statistics.trendsNum = await TrendsModel.countDocuments()
        // 获取所有动态评论数量
        statistics.trendsCommentNum = await TrendsCommentModel.countDocuments()
        // 获取用户数量
        const UserModel = require('../../model/UserModel')
        statistics.userNum = await UserModel.countDocuments()
        return statistics
    },
    notice: async(userID) => {
        const data = []
        // 查询当前用户的动态评论信息
        const trendsComment = await TrendsCommentModel.find({userID}).limit(20).sort('-createTime')
        data.push(...trendsComment)
        return data
    },
    addJournal: async(title, content, userID) => {
        await JournalModel.create({
            title,
            content,
            createTime: Date.now(),
            type: 1,
            view: false,
            userID
        })
    },
    journal: async(left,right,view,userID) => {
        let data = []
        if(view) {
            data = await JournalModel.find({
                type: 1,
                view: false,
                userID
            }).sort('-createTime')
        } else {
           data = await JournalModel.find({
                type: 1,
                userID
            }).sort('-createTime').skip(left).limit(right)
        }
        
        const total = await JournalModel.find({type: 1,view: false,userID}).countDocuments()
        return {
            data,
            total
        }
    },
    delJournal: async(id) => {
        await JournalModel.findByIdAndDelete(id)
    },
    readJournal: async(id) => {
        await JournalModel.findByIdAndUpdate(id, {
            view: true
        })
    },
    // 添加消息（评论通知等）
    addMessage: async(title,content,userID) => {
        await JournalModel.create({
            title,
            content,
            type: 2,
            view: false,
            createTime: Date.now(),
            userID
        })
    },
    message: async(left,right,view,userID) => {
        let data = []
        if(view) {
            data = await JournalModel.find({
                type: 2,
                view: false,
                userID
            }).sort('-createTime')
        } else {
            data = await JournalModel.find({
                type: 2,
                userID
            }).sort('-createTime').skip(left).limit(right)
        }
        return {
            data,
            total: await JournalModel.find({type: 2,view: false,userID}).countDocuments()
        }
    },
    readMessage: async(id) => {
        await JournalModel.findByIdAndUpdate(id, {
            view: true
        })
    },
    addVisit: async(data) => {
        await VisitLogModel.create({
            ...data,
            createTime: Date.now()
        })
        return await VisitLogModel.find().countDocuments()
    }
}
module.exports = dashboardService